﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.Xml;
using System.Text;

namespace ExpiredDomainSearch.Manager
{
    public class Common
    {
        public static string Serialize(SearchParam input)
        {
            XmlSerializer ser = new XmlSerializer(input.GetType());
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            ser.Serialize(writer, input);
            return sb.ToString();
        }

        public static SearchParam DeSerialize(string input)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(input);
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(input.GetType());
            object obj = ser.Deserialize(reader);
            return (SearchParam)obj;
        }

        public static string EncodeDecode(String Message, bool isencrypt)
        {
            String Key = "hritik26$";
            int messageCount, keyCount, value = 0;

            StringBuilder strRetVal = new StringBuilder();

            for (messageCount = 0, keyCount = 0; messageCount < Message.Length;
                    messageCount++, keyCount++)
            {
                if (keyCount >= Key.Length) keyCount = 0;

                if (isencrypt)
                    value = (Message[messageCount]) + (Key[keyCount]);
                else
                    value = (Message[messageCount]) - (Key[keyCount]);

                strRetVal.Append((char)(value));
            }

            return strRetVal.ToString();
        } 
    }
}